home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / edit / thesrc20.zip / directry.c < prev    next >
C/C++ Source or Header  |  1995-01-26  |  12KB  |  411 lines

  1. /***********************************************************************/
  2. /* DIRECTRY.C - Directory routines                                     */
  3. /***********************************************************************/
  4. /*
  5.  * THE - The Hessling Editor. A text editor similar to VM/CMS xedit.
  6.  * Copyright (C) 1991-1995 Mark Hessling
  7.  *
  8.  * This program is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU General Public License as
  10.  * published by the Free Software Foundation; either version 2 of
  11.  * the License, or any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16.  * General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to:
  20.  *
  21.  *    The Free Software Foundation, Inc.
  22.  *    675 Mass Ave,
  23.  *    Cambridge, MA 02139 USA.
  24.  *
  25.  *
  26.  * If you make modifications to this software that you feel increases
  27.  * it usefulness for the rest of the community, please email the
  28.  * changes, enhancements, bug fixes as well as any and all ideas to me.
  29.  * This software is going to be maintained and enhanced as deemed
  30.  * necessary by the community.
  31.  *
  32.  * Mark Hessling                     email: M.Hessling@gu.edu.au
  33.  * 36 David Road                     Phone: +61 7 849 7731
  34.  * Holland Park                      Fax:   +61 7 875 5314
  35.  * QLD 4121
  36.  * Australia
  37.  */
  38.  
  39. /*
  40. $Id: directry.c 2.0 1995/01/26 16:30:35 MH Release MH $
  41. */
  42. #ifdef __EMX__
  43. #  define __OS2__ 3
  44. #endif
  45.  
  46. #ifdef __OS2__
  47. #   define INCL_DOS
  48. #endif
  49. #include "the.h"
  50. #include "directry.h"
  51. #include "fnmatch.h"
  52. #include "proto.h"
  53.  
  54. static ATTR_TYPE curr_dirtype = (F_DI | F_AR);
  55. /*********************************************************************/
  56. #ifdef PROTO
  57. CHARTYPE *make_full(CHARTYPE *path, CHARTYPE *file)
  58. #else
  59. CHARTYPE *make_full(path, file)
  60. CHARTYPE *path, *file;
  61. #endif
  62. /*********************************************************************/
  63. {
  64.  static CHARTYPE filebuf[BUFSIZ];
  65.  short pathlen=strlen(path);
  66.  
  67.  if (pathlen+1+strlen(file)+1 > BUFSIZ)
  68.     return(NULL);
  69.  if (!strcmp(path, "") || !strcmp(path, "."))
  70.    {
  71.     (void) strcpy(filebuf, file);
  72.     return(filebuf);
  73.    }
  74.  (void) strcpy(filebuf, path);
  75.  if (*(path+(pathlen - 1)) != ISLASH && *file != ISLASH)
  76.     (void) strcat(filebuf, ISTR_SLASH);
  77.  (void) strcat(filebuf, file);
  78.  return(filebuf);
  79. }
  80. /*********************************************************************/
  81. #if defined(UNIX)
  82. #ifdef PROTO
  83. short getfiles(CHARTYPE *path,CHARTYPE *files,struct dirfile **dpfirst,
  84.                                     struct dirfile **dplast)
  85. #else
  86. short getfiles(path,files,dpfirst,dplast)
  87. CHARTYPE *path;
  88. CHARTYPE *files;
  89. struct dirfile **dpfirst;
  90. struct dirfile **dplast;
  91. #endif
  92. /*********************************************************************/
  93. {
  94.  DIR *dirp=NULL;
  95.  struct stat sp;
  96.  struct dirent *direntp=NULL;
  97.  struct dirfile *dp=NULL;
  98.  CHARTYPE *full_name=NULL;
  99.  short entries = 10;
  100.  
  101.  dirp = opendir(path);
  102.  if (dirp == NULL)
  103.     return(10);
  104.  
  105.  dp = *dpfirst = (struct dirfile *)(*the_calloc)(entries, sizeof(struct dirfile));
  106.  if (dp == NULL)
  107.     return(30);
  108.  *dplast = *dpfirst + entries;
  109.  
  110.  for (direntp = readdir(dirp);direntp != NULL;direntp = readdir(dirp))
  111.     {
  112.      if (fnmatch(files,direntp->d_name,0) == 0)
  113.        {
  114.         if ((full_name = make_full(path,(CHARTYPE *)direntp->d_name)) == NULL)
  115.            return(30);
  116.         if (stat(full_name,&sp) != 0)
  117.            continue;
  118.         if ((dp->fname = (CHARTYPE *)(*the_calloc)(strlen(direntp->d_name)+1,sizeof(CHARTYPE))) == NULL)
  119.            return(30);
  120.         strcpy(dp->fname,direntp->d_name);
  121.         dp->fattr = sp.st_mode;
  122.         dp->ftime = sp.st_mtime;
  123.         dp->fsize = sp.st_size;
  124.     dp->fdate = 0;
  125.         dp++;
  126.         if (dp == *dplast)
  127.           {
  128.            *dpfirst = (struct dirfile *)(*the_realloc)((CHARTYPE *)*dpfirst,
  129.                       2 * entries * sizeof (struct dirfile));
  130.            if (*dpfirst == NULL)
  131.               return(30);
  132.            dp = *dpfirst + entries;
  133.            *dplast = dp + entries;
  134.            entries *= 2;
  135.           }
  136.        }
  137.     }
  138.  closedir(dirp);
  139.  *dplast = dp;
  140.  return(0);
  141. }
  142. #else
  143. /*********************************************************************/
  144. #ifdef PROTO
  145. short getfiles(CHARTYPE *path,CHARTYPE *files,struct dirfile **dpfirst,
  146.                                     struct dirfile **dplast)
  147. #else
  148. short getfiles(path,files,dpfirst,dplast)
  149. CHARTYPE *path;
  150. CHARTYPE *files;
  151. struct dirfile **dpfirst;
  152. struct dirfile **dplast;
  153. #endif
  154. /*********************************************************************/
  155. {
  156.  struct dirfile *dp=NULL;
  157. #ifdef OS2
  158. #  ifdef __32BIT__
  159.  ULONG matches=1L;
  160.  ULONG rsvrd=FIL_STANDARD;
  161.  FILEFINDBUF3 ffblk;
  162. #  else
  163.  USHORT matches=1;
  164.  ULONG rsvrd=0;
  165.  FILEFINDBUF ffblk;
  166. #  endif
  167.  HDIR hdir=HDIR_SYSTEM;
  168. #else
  169.  FSTR_TYPE ffblk;
  170. #endif
  171.  ATTR_TYPE attrs=curr_dirtype;
  172.  DONE_TYPE done=0;
  173.  CHARTYPE *full_path=NULL;
  174.  CHARTYPE str_attr[11];
  175.  CHARTYPE str_date[10];
  176.  CHARTYPE str_time[6];
  177.  short entries = 10;
  178.  
  179.  if ((full_path = make_full(path,"*.*")) == NULL)
  180.        return(RC_FILE_NOT_FOUND);
  181.  
  182. #if defined(DOS) && defined(TC)
  183.  done = findfirst(full_path,&ffblk,attrs);
  184. #endif
  185. #if defined (DOS) && defined(MSC)
  186.  done = _dos_findfirst(full_path,attrs,&ffblk);
  187. #endif
  188. #if defined(DOS) && defined(GO32)
  189.  done = findfirst(full_path,&ffblk,attrs);
  190. #endif
  191. #ifdef OS2
  192. #  ifdef __32BIT__
  193.  done = DosFindFirst((PSZ) full_path, (PHDIR)&hdir, (ULONG)attrs,
  194.              (PVOID)&ffblk, (ULONG)sizeof(ffblk), (PULONG)&matches,
  195.              (ULONG)rsvrd);
  196. #  else
  197.  done = DosFindFirst((PSZ) full_path, (PHDIR)&hdir, (USHORT)attrs,
  198.              (PFILEFINDBUF)&ffblk, (USHORT)sizeof(ffblk), (PUSHORT)&matches,
  199.              (ULONG)rsvrd);
  200. #  endif
  201. #endif
  202.  if (done != 0)
  203.     return(RC_FILE_NOT_FOUND);
  204.  
  205.  dp = *dpfirst = (struct dirfile *)(*the_calloc)(entries, sizeof (struct dirfile));
  206.  *dplast = *dpfirst + entries;
  207.  
  208.  
  209.  while(!done)
  210.     {
  211.      if (fnmatch(files,ffblk.NAME_NAME,FNM_IGNORECASE) == 0)
  212.        {
  213.     if ((dp->fname = (CHARTYPE *)(*the_calloc)(strlen(ffblk.NAME_NAME)+1,sizeof(CHARTYPE))) == NULL)
  214.        return(RC_OUT_OF_MEMORY);
  215.     strcpy(dp->fname,ffblk.NAME_NAME);
  216.     dp->fattr = ffblk.ATTR_NAME;
  217.     dp->ftime = ffblk.TIME_NAME;
  218.     dp->fsize = ffblk.SIZE_NAME;
  219.     dp->fdate = ffblk.DATE_NAME;
  220.     dp++;
  221.     if (dp == *dplast)
  222.       {
  223.            *dpfirst = (struct dirfile *)(*the_realloc)((CHARTYPE *)*dpfirst,
  224.                       2 * entries * sizeof (struct dirfile));
  225.            if (*dpfirst == NULL)
  226.               return(RC_FILE_NOT_FOUND);
  227.            dp = *dpfirst + entries;
  228.            *dplast = dp + entries;
  229.            entries *= 2;
  230.           }
  231.        }
  232. #if defined(DOS) && defined(TC)
  233.      done = findnext(&ffblk);
  234. #endif
  235. #if defined(DOS) && defined(MSC)
  236.      done = _dos_findnext(&ffblk);
  237. #endif
  238. #if defined(DOS) && defined(GO32)
  239.      done = findnext(&ffblk);
  240. #endif
  241. #ifdef OS2
  242. #  ifdef __32BIT__
  243.      done = DosFindNext((HDIR)hdir, (PVOID)&ffblk, (ULONG)sizeof(ffblk),
  244.             (PULONG)&matches);
  245. #  else
  246.      done = DosFindNext((HDIR)hdir, (PFILEFINDBUF)&ffblk, (USHORT)sizeof(ffblk),
  247.             (PUSHORT)&matches);
  248. #  endif
  249. #endif
  250.     }
  251.  *dplast = dp;
  252.  return(RC_OK);
  253. }
  254. #endif
  255. /*********************************************************************/
  256. #ifdef PROTO
  257. int fcomp(struct dirfile *first,struct dirfile* next)
  258. #else
  259. int fcomp(first,next)
  260. struct dirfile *first;
  261. struct dirfile* next;
  262. #endif
  263. /*********************************************************************/
  264. {
  265. #ifdef OS2
  266.  return(stricmp(first->fname,next->fname));
  267. #else
  268.  return(strcmp(first->fname,next->fname));
  269. #endif
  270. }
  271. /*********************************************************************/
  272. #ifdef PROTO
  273. CHARTYPE *file_date(D_TYPE date,CHARTYPE *str_date)
  274. #else
  275. CHARTYPE *file_date(date,str_date)
  276. D_TYPE date;
  277. CHARTYPE *str_date;
  278. #endif
  279. /*********************************************************************/
  280. {
  281.  static CHARTYPE mon[12][4] = {
  282.  "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
  283.  sprintf(str_date,"%2d-%3.3s-%2.2d",DAYS_MASK,MONT_MASK,YEAR_MASK);
  284.  return(str_date);
  285. }
  286. /*********************************************************************/
  287. #ifdef PROTO
  288. CHARTYPE *file_time(T_TYPE time,CHARTYPE *str_time)
  289. #else
  290. CHARTYPE *file_time(time,str_time)
  291. T_TYPE time;
  292. CHARTYPE *str_time;
  293. #endif
  294. /*********************************************************************/
  295. {
  296.  sprintf(str_time,"%2d:%2.2d",HOUR_MASK,MINU_MASK);
  297.  return(str_time);
  298. }
  299. /*********************************************************************/
  300. #ifdef PROTO
  301. CHARTYPE *file_attrs(ATTR_TYPE attrs,CHARTYPE *str_attr)
  302. #else
  303. CHARTYPE *file_attrs(attrs,str_attr)
  304. ATTR_TYPE attrs;
  305. CHARTYPE *str_attr;
  306. #endif
  307. /*********************************************************************/
  308. {
  309. #ifdef UNIX
  310.  ATTR_TYPE ftype=(attrs & S_IFMT);
  311.  
  312.  str_attr[10] = '\0';
  313.  switch(ftype)
  314.    {
  315.     case S_IFDIR:  str_attr[0] = 'd'; break;
  316.     case S_IFCHR:  str_attr[0] = 'c'; break;
  317.     case S_IFIFO:  str_attr[0] = 'p'; break;
  318. #if defined(S_IFSOCK)
  319.     case S_IFSOCK: str_attr[0] = 's'; break;
  320. #endif
  321. #if defined(S_IFLNK)
  322.     case S_IFLNK:  str_attr[0] = 'l'; break;
  323. #endif
  324.     default:       str_attr[0] = '-'; break;
  325.    }
  326.  str_attr[1] = (attrs & S_IRUSR) ? 'r' : '-';
  327.  str_attr[2] = (attrs & S_IWUSR) ? 'w' : '-';
  328.  str_attr[3] = (attrs & S_IXUSR) ? 'x' : '-';
  329.  str_attr[3] = (attrs & S_ISUID) ? 's' : str_attr[3];
  330.  str_attr[4] = (attrs & S_IRGRP) ? 'r' : '-';
  331.  str_attr[5] = (attrs & S_IWGRP) ? 'w' : '-';
  332.  str_attr[6] = (attrs & S_IXGRP) ? 'x' : '-';
  333.  str_attr[6] = (attrs & S_ISGID) ? 's' : str_attr[6];
  334.  str_attr[7] = (attrs & S_IROTH) ? 'r' : '-';
  335.  str_attr[8] = (attrs & S_IWOTH) ? 'w' : '-';
  336.  str_attr[9] = (attrs & S_IXOTH) ? 'x' : '-';
  337. #else
  338.  strcpy(str_attr,".... ");
  339.  if ((attrs & F_RO) == F_RO)
  340.    str_attr[0] = 'r';
  341.  if ((attrs & F_AR) == F_AR)
  342.    str_attr[1] = 'a';
  343.  if ((attrs & F_SY) == F_SY)
  344.    str_attr[2] = 's';
  345.  if ((attrs & F_HI) == F_HI)
  346.    str_attr[3] = 'h';
  347.  str_attr[4] = '\0';
  348.  if ((attrs & F_DI) == F_DI)
  349.    strcat(str_attr,"(dir) ");
  350.  else
  351.    strcat(str_attr,"      ");
  352. #endif
  353.  return(str_attr);
  354. }
  355. /*********************************************************************/
  356. #ifdef PROTO
  357. short set_dirtype(CHARTYPE *params)
  358. #else
  359. short set_dirtype(params)
  360. CHARTYPE *params;
  361. #endif
  362. /***********************************************************************/
  363. {
  364. /*--------------------------- local data ------------------------------*/
  365. #define NUM_DIRTYPE 5
  366.  static CHARTYPE *dirtype[NUM_DIRTYPE] =
  367.          {(CHARTYPE *)"normal",
  368.           (CHARTYPE *)"readonly",
  369.           (CHARTYPE *)"system",
  370.           (CHARTYPE *)"hidden",
  371.           (CHARTYPE *)"directory"};
  372.  static ATTR_TYPE att[NUM_DIRTYPE] =
  373.          {0,F_RO,F_SY,F_HI,F_DI};
  374.  CHARTYPE *p=NULL;
  375.  register short i=0;
  376.  ATTR_TYPE attribs=0;
  377.  bool found=FALSE;
  378. /*--------------------------- processing ------------------------------*/
  379.  if (strcmp(params,"") == 0)                 /* error if no paramaters */
  380.    {
  381.     display_error(3,(CHARTYPE *)"",FALSE);
  382.     return(RC_INVALID_OPERAND);
  383.    }
  384.  if (strcmp(params,"*") == 0)                            /* set to ALL */
  385.    {
  386.     curr_dirtype = (F_RO | F_HI | F_SY | F_DI | F_AR);
  387.     return(RC_OK);
  388.    }
  389.  p = (CHARTYPE *)strtok(params," ");
  390.  while(p != NULL)
  391.    {
  392.     found = FALSE;
  393.     for (i=0;i<NUM_DIRTYPE;i++)
  394.        {
  395.         if (equal(dirtype[i],p,1))
  396.           {
  397.            found = TRUE;
  398.            attribs |= att[i];
  399.           }
  400.        }
  401.     if (!found)
  402.       {
  403.        display_error(1,(CHARTYPE *)p,FALSE);
  404.        return(RC_INVALID_OPERAND);
  405.       }
  406.     p = (CHARTYPE *)strtok(NULL," ");
  407.    }
  408.  curr_dirtype = attribs;
  409.  return(RC_OK);
  410.  }
  411.